<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//结账
class Checkout extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('Chuna/CashBalanceModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 结账
	* @param date_month ture char 结账月份，格式2019-05
	* @return 成功返回1
	*/
	//end
	function balance(){
		
		if(!$this->check_priv('chuna_balance_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$user_id = $this->user_id;
		
		$date_month = $this->input->post('date_month',null,'');
		if(empty($date_month)){
			$this->err_out('请选择结账月份');
		}
		$date_month = date('Y-m',strtotime($date_month.'-01'));
		if($date_month<'2000-01-01'){
			$this->err_out('所选时间段不在可结算时间范围内');
		}
		$date = date('Y-m');
		
		if($date_month>$date){
			$this->err_out('所选时间段不在可结算时间范围内');
		}

		//检查会计期结帐，要想出纳结账，会计期的月份(出纳当前期的上一个月)必须已结账
		$last_month = date('Y-m',strtotime('-1 month',strtotime($date_month)));
		$this->load->model('Accounting/AccountingBalanceModel','AcBaModel');
		$abinfo = $this->AcBaModel->_where_exist(array('date_month'=>$last_month,'status'=>0));
		if(!$abinfo){
			$this->err_out('所选时间段已到会计期结账，请先会计期结账');
		}
		
		$is_exist = $this->CurModel->_where_exist(array('date_month'=>$date_month,'status'=>0));
		if($is_exist){
			$this->err_out('所选时间段已经结账，请检查时间是否有误。');
		}
		
		$prev = $this->CurModel->get_prev_balance($date_month);
		if($prev){
			$prev_money = $prev['money'];
		}
		else{
			$prev_money = 0;
		}
		
		
		
		$this->load->model('Chuna/cashiersModel','cashiersModel');
		//$cash_list = $this->cashiersModel->get_list($date_month);
		
		$income = $this->cashiersModel->get_totals(array('date_month'=>$date_month),'income');
		$expenses = $this->cashiersModel->get_totals(array('date_month'=>$date_month),'expenses');
		
		
		$money = $income - $expenses;
		
		$time = time();
		
		$row = $this->CurModel->getRow(array('date_month'=>$date_month));
		$arr = array(
			'user_id'=>$user_id,
			'date_month'=>$date_month,
			'addtime'=>$time,
			'income'=>$income,
			'expenses'=>$expenses,
			'money'=>$money,
			'prev_money'=>$prev_money,
			'status'=>0
		);
		if(!$row){
			$this->CurModel->add($arr);
		}
		else{
			$this->CurModel->edit($arr,"id='{$row['id']}'");
		}
		
		
		
		$this->load->model('System/Msg_templateModel','Msg_templateModel');
		$this->Msg_templateModel->notice_handle($user_id,'chuna_qimo',$arr);
		
		
		$this->add_log("对【{$date_month}】的账目进行结账操作",'cash');
		

		$next_month = date('Y-m',strtotime('+1 month',strtotime($date_month)));
		$this->data_out($next_month);
		
	}
	//start
	/**
	* @name 反结账
	* @param date_month ture char 结账月份，格式2019-05
	* @return 成功返回1
	*/
	//end
	function balance_fan(){
		
		if(!$this->check_priv('chuna_fan_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		
		$user_id = $this->user_id;
		
		$date_month = $this->input->post('date_month',null,'');
		if(empty($date_month)){
			$this->err_out('请选择反结账月份');
		}
		$date_month = date('Y-m',strtotime($date_month.'-01'));
		if($date_month<'2000-01-01'){
			$this->err_out('所选时间段不在可反结账时间范围内');
		}
		
		$info = $this->CurModel->getRow(array('date_month'=>$date_month));
		
		if(!$info){
			$this->err_out('所选时间段尚未结账，请检查时间是否有误');
		}
		if($info['status']!=0){
			$this->err_out('所选时间段已经反结账，请检查时间是否有误');
		}
		
		//判断是否会计结账,当accountion_balance表有出纳结账月份的数据时,出纳是不能对该月分进行反结账的
		$this->load->model('Accounting/AccountingBalanceModel','AcBaModel');
		$abinfo = $this->AcBaModel->getRow(array('date_month'=>$date_month));
		if($abinfo && $abinfo['status'] == 0 && $info['status'] == 0){
			$this->err_out('所选时间段已到会计期反结账，当前不能进行出纳期反结账');
		}
		//判断权限是否可以操作
		/*
		if(0){
			$this->err_out('权限不足，不可操作');
		}
		*/
		
		$this->CurModel->edit(array('status'=>1),"id='{$info['id']}'");
		
		$this->add_log("对【{$date_month}】的账目进行反结账操作",'cash');
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 查询是否已结账
	* @param date_month ture char 结账月份，格式2019-05
	* @return 成功返回1
	*/
	//end
	function getBalance(){
		
		$row = $this->CurModel->get_last_balance();
		
		if(!$row){
			$cash_balance = date("Y-m",time());
		}
		else{
			$cash_balance = date("Y-m",strtotime("+1 months",strtotime($row['date_month'])));
		}

		$this->load->model('Accounting/AccountingBalanceModel','AcBaModel');
		$row2 = $this->AcBaModel->get_last_balance();
		if(!$row2){
			$account_balance = date("Y-m",time());
		}
		else{
			$account_balance = date("Y-m",strtotime("+1 months",strtotime($row2['date_month'])));
		}

		$result = array(
			'cash_balance' => $cash_balance,
			'account_balance' => $account_balance
		);

		$this->data_out($result);
	}
}
